package com.app.base.adapter;


import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.app.base.adapter.holder.BaseHolder;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseHolder<T>> {

	//与View绑定的ViewHolder
	protected Class<? extends BaseHolder<T>> clazz;
	//item的布局资源文件
	protected int viewResId;
	
	protected int mResHeadViewId = -1;
	protected int mResFootViewId = -1;
	
	protected List<T> mItems;

	public BaseRecyclerViewAdapter(int viewRes,
			Class<? extends BaseHolder<T>> holderClazz) {
		this.viewResId = viewRes;
		this.clazz = holderClazz;
	}

	public List<T> getItems() {
		return mItems;
	}

	public void setDatas(List<T> items) {
		if(mItems == items){
			notifyDataSetChanged();
			return ;
		}
		if(mItems !=null){
			mItems.clear();
			mItems = null;
		}
		mItems = items;
		notifyDataSetChanged();
	}
	
	public void setDatas(List<T> items,boolean isClear) {
		if(isClear){
			setDatas(items);
		}else {
			mItems = items;
			notifyDataSetChanged();
		}
	}

	protected T getItem(int position) {
		if (mItems == null)
			return null;
		return mItems.get(position);
	}
	public void removeItem(int position) {
		if(mItems!=null){
			this.mItems.remove(position);
			notifyItemRemoved(position);
			notifyItemRangeChanged(position, getItemCount() - position);
		}

	}
	
	/**
	 * 清除列表数据和脚View
	 */
	public void clear() {
		if (mItems == null)return;
		int size  =  mItems.size();
		if(mResHeadViewId!=-1){
			if(mResFootViewId!=-1){
				notifyItemRangeRemoved(1, size+1);
			}else {
				notifyItemRangeRemoved(1, size);
			}
			
		}else {
			if(mResFootViewId!=-1){
				notifyItemRangeRemoved(0, size+1);
			}else {
				notifyItemRangeRemoved(0, size);
			}
		}
		mItems.clear();
		mItems=null;
	
	}

	public boolean addAll(final Collection<? extends T> collection) {
	
		if(collection == null){
			return false ;
		}
		if (mItems == null)mItems = new ArrayList<>();
		int positionStart = mItems.size() - 1;
		positionStart = (positionStart<0?0:positionStart);
		boolean result = mItems.addAll(collection);
		if(mResHeadViewId != -1){
			notifyItemRangeInserted(positionStart+2, collection.size());
		}else {
			
			notifyItemRangeInserted(positionStart+1, collection.size());
		}
		return result;
	}

	public void add(final int index, @NonNull final T item) {
		if (mItems == null)mItems = new ArrayList<>();
		mItems.add(index, item);
		notifyItemInserted(index);
		
	}

	public boolean add(@NonNull final T object) {
		if (mItems == null)mItems = new ArrayList<>();
		boolean result = mItems.add(object);
		notifyItemInserted(mItems.size());
		return result;
	}

	@Override
	public int getItemCount() {
		if (mItems == null)
			return 0;
		return mItems.size();
	}

	@Override
	public void onBindViewHolder(BaseHolder<T> viewHolder, int position) {
		viewHolder.setData(mItems.get(position),position);
		viewHolder.getRootView().setTag(position);
	}

	@Override
	public BaseHolder<T> onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		View view = LayoutInflater.from(viewGroup.getContext()).inflate(
				viewResId, viewGroup, false);
		return getBaseHolder(view, clazz);
	}

	protected BaseHolder<T> getBaseHolder(View view,
			Class<? extends BaseHolder<T>> clazz) {
		BaseHolder<T> holder = null;
		try {
			Constructor<? extends BaseHolder<T>> constructor = clazz
					.getConstructor(View.class);
			holder = constructor.newInstance(view);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return holder;
	}
}
